package com.jwsoft.manager.core.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.module.base.common.vo.dict.DictQueryVO;
import com.bifang.module.base.core.integration.DictIntegration;
import com.bifang.module.system.core.dao.service.SysApplicationService;
import com.jwsoft.manager.common.enums.SchoolCategoryEnum;
import com.jwsoft.manager.common.vo.eduSyncJhDelete.EduSyncExistInfo;
import com.jwsoft.manager.common.vo.eduSyncJhDelete.EduSyncJhDeleteQueryVO;
import com.jwsoft.manager.core.dao.model.EduSyncJhApply;
import com.jwsoft.manager.core.dao.model.EduSyncJhApprove;
import com.jwsoft.manager.core.dao.model.EduSyncJhDelete;
import com.jwsoft.manager.core.dao.mapper.EduSyncJhDeleteMapper;
import com.jwsoft.manager.core.dao.model.EduSyncJhEnroll;
import com.jwsoft.manager.core.dao.service.EduSyncJhApplyService;
import com.jwsoft.manager.core.dao.service.EduSyncJhApproveService;
import com.jwsoft.manager.core.dao.service.EduSyncJhDeleteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwsoft.manager.core.dao.service.EduSyncJhEnrollService;
import com.jwsoft.manager.core.integration.EduSyncJhApplyIntegration;
import com.jwsoft.manager.core.integration.EduSyncJhApproveIntegration;
import com.jwsoft.manager.core.integration.EduSyncJhEnrollIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 金华市报名删除同步情况表服务实现类
 *
 * @author zhiyuan
 * @since 2024-04-08
 */
@Service
public class EduSyncJhDeleteServiceImpl extends ServiceImpl<EduSyncJhDeleteMapper, EduSyncJhDelete> implements EduSyncJhDeleteService {

    @Autowired
    private EduSyncJhApproveService approveService;

    @Autowired
    private EduSyncJhApplyService applyService;

    @Autowired
    private DictIntegration dictIntegration;
    @Autowired
    private EduSyncJhEnrollService enrollService;

    @Autowired
    private EduHelper eduHelper;

    @Override
    public EduSyncExistInfo querySyncInfo(EduSyncJhDeleteQueryVO vo) {
        if(vo.getYear() == null){
            Integer year = eduHelper.thisYear();
            vo.setYear(year);
        }

        if (StringUtils.isBlank(vo.getIdcard())) {
            throw new AppException("证件号码不能为空！");
        }
        if (StringUtils.isEmpty(vo.getName())){
            throw new AppException("姓名不能为空！");
        }
        if (StringUtils.isEmpty(vo.getApplyCountyCode())){
            throw new AppException("报名所在区县码不能为空！");
        }
        EduSyncExistInfo result = new EduSyncExistInfo();
        //查询报名表是否有数据
        QueryWrapper<EduSyncJhApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduSyncJhApply::getIdcard, vo.getIdcard())
                .eq(EduSyncJhApply::getYear, vo.getYear())
                .eq(EduSyncJhApply::getName, vo.getName());
        List<EduSyncJhApply> syncJhApplyList = applyService.list(queryWrapper);
        if (CollectionUtils.isEmpty(syncJhApplyList)){
            result.setRemark("暂无报名数据！");
        }else{
            List<Long> applyIdList= syncJhApplyList.stream().map(EduSyncJhApply::getJhApplyId).collect(Collectors.toList());
            QueryWrapper<EduSyncJhApprove> approveQueryWrapper = new QueryWrapper<>();
            approveQueryWrapper.lambda().select(EduSyncJhApprove::getJhApproveId,EduSyncJhApprove::getJhApplyId,EduSyncJhApprove::getApplyCountyCode,EduSyncJhApprove::getApproveState).eq(EduSyncJhApprove::getIdcard, vo.getIdcard())
                    .eq(EduSyncJhApprove::getYear, vo.getYear())
                    .in(EduSyncJhApprove::getJhApplyId,applyIdList)
                    .eq(EduSyncJhApprove::getName, vo.getName());
            List<EduSyncJhApprove> approveList = approveService.list(approveQueryWrapper);
            List<EduSyncJhEnroll> enrollList=new ArrayList<>();
            Map<Long,EduSyncJhApprove> approveMap=new HashMap<>();
            Map<Long,EduSyncJhEnroll> enrollMap=new HashMap<>();
            if(!CollectionUtils.isEmpty(approveList)){
                approveMap=approveList.stream().collect(Collectors.toMap(EduSyncJhApprove::getJhApplyId, a -> a,(k1,k2)->k1));
                QueryWrapper<EduSyncJhEnroll> enrollQueryWrapper = new QueryWrapper<>();
                enrollQueryWrapper.lambda().select(EduSyncJhEnroll::getJhEnrollId,EduSyncJhEnroll::getJhApplyId,EduSyncJhEnroll::getApplyCountyCode,EduSyncJhEnroll::getEnrollState).eq(EduSyncJhEnroll::getIdcard, vo.getIdcard())
                        .eq(EduSyncJhEnroll::getYear, vo.getYear())
                        .eq(EduSyncJhEnroll::getEnrollState,"1")
                        .eq(EduSyncJhEnroll::getName, vo.getName());
                enrollList = enrollService.list(enrollQueryWrapper);
                if (!CollectionUtils.isEmpty(enrollList)){
                    enrollMap=enrollList.stream().collect(Collectors.toMap(EduSyncJhEnroll::getJhApplyId, a -> a,(k1,k2)->k1));
                }
            }
            EduSyncJhApply syncJhApply=new EduSyncJhApply();
            //报名状态
            result.setType("1");
            int approve4Cnt=0;
            for(EduSyncJhApply apply:syncJhApplyList){
                EduSyncJhApprove approve=approveMap.get(apply.getJhApplyId());
                EduSyncJhEnroll enroll=enrollMap.get(apply.getJhApplyId());
                String type="1";
                if(approve!=null){
                    if("1".equals(approve.getApproveState())){
                        if(enroll!=null){
                            if("1".equals(enroll.getEnrollState())){
                                type="3";

                            }else{
                                type="4";
                            }
                        }else{
                            type="2";
                        }

                    }else{
                        type="4";

                    }
                }else{
                    type="1";
                }
                if("4".equals(type)){
                    approve4Cnt++;
                }
                if("3".equals(type)){
                    result.setType("3");
                    BeanUtil.copyProperties(apply,syncJhApply);
                    break;
                }
            }
            if(approve4Cnt==syncJhApplyList.size()){
                syncJhApply=syncJhApplyList.get(0);
                result.setType("4");
                return result;
            }else{
                if(!"3".equals(result.getType())){
                    for(EduSyncJhApply apply:syncJhApplyList){
                        EduSyncJhApprove approve=approveMap.get(apply.getJhApplyId());
                        EduSyncJhEnroll enroll=enrollMap.get(apply.getJhApplyId());
                        if(approve!=null&&enroll==null){
                            if("1".equals(approve.getApproveState())){
                                result.setType("2");
                                if (syncJhApply.getJhApplyId()==null){
                                    BeanUtil.copyProperties(apply,syncJhApply);
                                }else {
                                    if (!vo.getApplyCountyCode().equals(syncJhApply.getApplyCountyCode())) {
                                        BeanUtil.copyProperties(apply,syncJhApply);
                                        break;
                                    }
                                }

                            }
                        }
                    }
                    if(!"2".equals(result.getType())){
                        for(EduSyncJhApply apply:syncJhApplyList){
                            EduSyncJhApprove approve=approveMap.get(apply.getJhApplyId());
                            if(approve==null){
                                result.setType("1");
                                if (syncJhApply.getJhApplyId()==null){
                                    BeanUtil.copyProperties(apply,syncJhApply);
                                }else {
                                    if (!vo.getApplyCountyCode().equals(syncJhApply.getApplyCountyCode())) {
                                        BeanUtil.copyProperties(apply,syncJhApply);
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }

            result.setName(syncJhApply.getName());
            result.setIdcard(syncJhApply.getIdcard());
            result.setApplyCountyCode(syncJhApply.getApplyCountyCode());
            if (StringUtils.isNotBlank(syncJhApply.getApplyCountyCode())){
                DictQueryVO dictQueryVO = new DictQueryVO();
                dictQueryVO.setDictField("AREA");
                dictQueryVO.setDictKey(syncJhApply.getApplyCountyCode());
                String areaName = dictIntegration.getValue(dictQueryVO);
                vo.setApplyCountyName(areaName);
            }
            if (StringUtils.isNotBlank(syncJhApply.getSchoolCategory())){
                if (SchoolCategoryEnum.JUNIOR.getType().equalsIgnoreCase(syncJhApply.getSchoolCategory())){
                    result.setSchoolCategoryName(SchoolCategoryEnum.JUNIOR.getName());
                }
                if (SchoolCategoryEnum.PRIMARY.getType().equalsIgnoreCase(syncJhApply.getSchoolCategory())){
                    result.setSchoolCategoryName(SchoolCategoryEnum.PRIMARY.getName());
                }
                if (SchoolCategoryEnum.KINDERGARTEN.getType().equalsIgnoreCase(syncJhApply.getSchoolCategory())){
                    result.setSchoolCategoryName(SchoolCategoryEnum.KINDERGARTEN.getName());
                }
            }
            //接口返回参数
            result.setYear(syncJhApply.getYear());
            result.setApplyCountyName(syncJhApply.getApplyCountyName());
            result.setApplyEduName(vo.getApplyCountyName()+"教育局");
        }
        return result;
    }
}
